#!/bin/bash

echo "=== Kafka 大批量数据处理 Demo 启动脚本 ==="
echo ""

# 检查Docker是否运行
if ! docker info > /dev/null 2>&1; then
    echo "错误: Docker未运行，请先启动Docker"
    exit 1
fi

# 启动Kafka环境
echo "1. 启动Kafka环境 (Zookeeper + Kafka + Kafka UI)..."
docker-compose up -d

# 等待Kafka启动
echo "2. 等待Kafka启动..."
sleep 30

# 检查Kafka是否就绪
echo "3. 检查Kafka状态..."
docker-compose ps

# 创建主题
echo "4. 创建Kafka主题..."
docker exec kafka kafka-topics --create --topic user-events --bootstrap-server localhost:9092 --partitions 3 --replication-factor 1 --if-not-exists

# 验证主题创建
echo "5. 验证主题创建..."
docker exec kafka kafka-topics --list --bootstrap-server localhost:9092

echo ""
echo "=== 环境启动完成 ==="
echo ""
echo "服务地址:"
echo "- Kafka: localhost:9092"
echo "- Kafka UI: http://localhost:8080"
echo ""
echo "下一步:"
echo "1. 运行应用: mvn spring-boot:run"
echo "2. 或者运行测试: ./test-demo.sh"
echo ""
echo "停止环境: docker-compose down"
echo ""
